//网站引用的JS

(function(){        //闭包
    function load_script(xyUrl, callback){
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = xyUrl;
        //借鉴了jQuery的script跨域方法
        script.onload = script.onreadystatechange = function(){
            if((!this.readyState || this.readyState === "loaded" || this.readyState === "complete")){
                callback && callback();
                // Handle memory leak in IE
                script.onload = script.onreadystatechange = null;
                if ( head && script.parentNode ) {
                    head.removeChild( script );
                }
            }
        };
        // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
        head.insertBefore( script, head.firstChild );
    }
    function translate(point,type,callback){
        var callbackName = 'cbk_' + Math.round(Math.random() * 10000);    //随机函数名
        var xyUrl = "http://api.map.baidu.com/ag/coord/convert?from="+ type + "&to=4&x=" + point.lng + "&y=" + point.lat + "&callback=BMap.Convertor." + callbackName;
        //动态创建script标签
        load_script(xyUrl);
        BMap.Convertor[callbackName] = function(xyResult){
            delete BMap.Convertor[callbackName];    //调用完需要删除改函数
            var point = new BMap.Point(xyResult.x, xyResult.y);
            callback && callback(point);
        }
    }

    window.BMap = window.BMap || {};
    BMap.Convertor = {};
    BMap.Convertor.translate = translate;
})();
/**
 * Created by Administrator on 2016/9/5.
 */




//地图配置各项
$(function(){
    navigator.geolocation.getCurrentPosition(translatePoint); //定位 回调函数
});
function translatePoint(position){
    var currentLat = position.coords.latitude;
    var currentLon = position.coords.longitude;
    var gpsPoint = new BMap.Point(currentLon, currentLat);
    BMap.Convertor.translate(gpsPoint, 0, initMap); //转换坐标
}

//初始化地图
function initMap(point){
    map = new BMap.Map("mapBox");
    map.addControl(new BMap.NavigationControl());
    map.addControl(new BMap.ScaleControl());
    map.addControl(new BMap.OverviewMapControl());
    map.centerAndZoom(point, 8);   //当前点以及地图放大级别
    map.enableScrollWheelZoom()
//            map.addOverlay(new BMap.Marker(point))



//          自定义大头针
    var icon1 = new BMap.Icon('start.png', new BMap.Size(26,37));//起点大头针
    var icon2 = new BMap.Icon('finish.png', new BMap.Size(26,37));//终点大头针

//            大头针定位
//            var point1 = new BMap.Point();
    var mkr1 = new BMap.Marker(point, {icon: icon1});   //起点定位
    var point2 = new BMap.Point(116.447804,39.913123);   //终点坐标
    var mkr2 = new BMap.Marker(point2,{icon:icon2});   //终点定位

//            折线
//            var polyline = new BMap.Polyline([mkr1,mkr2],{strokeColor:"blue",strokeWeight:6,strokeOpacity:0.5});

    map.addOverlay(mkr1);
    map.addOverlay(mkr2);

//            定位点连线

    mkr1.setAnimation(BMAP_ANIMATION_DROP ); //跳动的动画
    mkr2.setAnimation(BMAP_ANIMATION_DROP ); //跳动的动画
    var polyline = new BMap.Polyline([
        point,
//                new BMap.Point(116.401801,39.912114),
//                new BMap.Point(115.403803,39.912336),
//                new BMap.Point(114.404804,39.912447),
//                new BMap.Point(113.405905,39.918558),
//                new BMap.Point(112.418804,39.926123),
//                new BMap.Point(111.419184,39.929323),
//                new BMap.Point(110.423184,39.930323),
        point2
    ],{strokeColor:"red", strokeWeight:3, strokeOpacity:0.5});

    map.addOverlay(polyline);

}

/**
 * Created by Administrator on 2016/9/5.
 */
